import Utils
import os

InputGameApk = 'E:\\game\\resdir\\apk'
OutputGameApk = 'E:\\game\\kfsz_game\\assets\\gamedir\\game'

InputIcon = 'E:\\game\\resdir\\image\\ic_launcher.png'
OutputIcon = 'E:\\game\\kfsz_game\\res\\mipmap-xxxhdpi-v4\\ic_launcher.png'

InputSplash = 'E:\\game\\resdir\\image\\splash.png'
OutputSplash = 'E:\\game\\kfsz_game\\res\\drawable-hdpi-v4\\splash.png'

InputConf = 'E:\\game\\resdir\\config.txt'

GameStrings = 'E:\\game\\kfsz_game\\res\\values\\strings.xml'

Manifest = 'E:\\game\\kfsz_game\\AndroidManifest.xml'

PackPath = 'E:\\game\\kfsz_game\\smali\\com\\kfzs\\kemllor'

KfzsPath = 'E:\\game\\kfsz_game\\assets\\kfzs.properties'


# 移动资源文件
def moveRes():
    if os.path.isdir(InputGameApk):
        apkName = os.listdir(InputGameApk)[0]
        if Utils.IsExists(InputGameApk):
            Utils.copyFile(InputGameApk + '\\' + apkName, OutputGameApk)
            print('移动%s.apk成功' % (apkName))

    if Utils.IsExists(InputIcon):
        Utils.copyFile(InputIcon, OutputIcon)
        print('移动ic_launcher.png成功')

    if Utils.IsExists(InputSplash):
        Utils.copyFile(InputSplash, OutputSplash)
        print('移动splash.png成功')


# 修改配置文件
def changeConf():
    if Utils.IsExists(InputConf):
        f = open(InputConf, 'r+')
        flist = f.readlines()
        f.close()

        gameName = flist[0][10:len(flist[2])].rstrip()
        changeStringsName(gameName)
        print("修改应用名称为:", gameName)

        pathName = flist[1][10:len(flist[2])].rstrip()
        changePath(pathName)
        print('路径修改成功:', pathName)

        packName = flist[2][18:len(flist[2])].rstrip()
        direction = flist[3]
        if "竖屏" in direction:
            direction = 'portrait'
        else:
            direction = 'landscape'
        changeManifest(packName, direction, pathName)
        print("修改应用包名为:", packName)
        changeKfzsPackName(packName)
        print("修改kfzs.properties内包名为:", packName)
        print("修改应用Activity（portrait==竖屏:landscape=横屏）方向为:", direction)


# 修改appName
def changeStringsName(name):
    if Utils.IsExists(GameStrings):
        Utils.create_app_name(GameStrings, name)


# 修改Manifest
def changeManifest(name, direction, pathName):
    if Utils.IsExists(Manifest):
        Utils.change_app_pack_name(Manifest, name, direction, pathName)


# 修改路径名称以及smali
def changePath(name):
    Utils.change_path(PackPath, name)


# 修改kfzs.properties的包名
def changeKfzsPackName(packName):
    Utils.change_kfzs_pack_name(KfzsPath, packName)


if __name__ == '__main__':
    moveRes()
    changeConf()
